package name.mrzc.licensing.template;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import name.mrzc.licensing.db.ILicenseDB;
import name.mrzc.licensing.model.License;

@Component
public class LicenseTemplate implements ILicenseDB{
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public List<License> findByOrganizationId(String organizationId) {
		List<License> licenses;

		licenses = jdbcTemplate.query("select * from licenses where organization_id = ?", new RowMapper<License>() {
			@Override
			public License mapRow(ResultSet rs, int rowNum) throws SQLException {
				License license = new License();
				license.setLicenseId(rs.getString("license_id"));
				license.setOrganizationId(rs.getString("organization_id"));
				license.setProductName(rs.getString("product_name"));
				license.setLicenseType(rs.getString("license_type"));
				license.setLicenseMax(rs.getInt("license_max"));
				license.setLicenseAllocated(rs.getInt("license_allocated"));
				license.setComment(rs.getString("comment"));
				return license;
			}
		}, organizationId);

		return licenses;
	}

	@Override
	public License findByOrganizationIdAndLicenseId(String organizationId, String licenseId) {
		// TODO Auto-generated method stub
		return null;
	}

}
